import { redirect, img, getToken } from '@/utils/common';
export function useGoods(params: any = {}) {

	const baseTagStyle = (data: any) => {
		let style = "";
		if (data.color_json.text_color) {
			style += `color:${data.color_json.text_color};`;
		}
		if (data.color_json.border_color) {
			style += `border-color: ${data.color_json.border_color};`;
		}
		if (data.color_json.bg_color) {
			style += `background-color: ${data.color_json.bg_color};`;
		}
		return style;
	}

	// 价格类型
	const priceType = (data: any) => {
		let type = "";
		if (data.member_discount && getToken() && data.goodsSku.member_price != data.goodsSku.price) {
			type = 'member_price' // 会员价
		}
		return type;
	}

	// 商品价格
	const goodsPrice = (data: any) => {
		// let price = "0.00";
		let price = 0;
		if (data.member_discount && getToken() && data.goodsSku.member_price != data.goodsSku.price) {
			price = data.goodsSku.member_price ? data.goodsSku.member_price : data.goodsSku.price // 会员价
		} else {
			price = data.goodsSku ? data.goodsSku.price : data.price; //兼容商品推荐组件
		}
		return parseFloat(fen2yuan(price));
	}
	/**
	 * 拼接营销信息
	 */
	const appendSettlementProduct = (spus: any, settlementInfos: any[]) => {
		if (!settlementInfos || settlementInfos.length === 0) {
			return;
		}
		for (const spu of spus) {
			const settlementInfo = settlementInfos.find((info: { spuId: any; }) => info.spuId === spu.spuId);
			if (!settlementInfo) {
				return;
			}
			// 选择价格最小的 SKU 设置到 SPU 上
			const settlementSku = settlementInfo.skus
				.filter((sku: { promotionPrice: number; }) => sku.promotionPrice > 0)
				.reduce((prev: { promotionPrice: number; }, curr: { promotionPrice: number; }) => (prev.promotionPrice < curr.promotionPrice ? prev : curr));
			if (settlementSku) {
				spu.promotionType = settlementSku.promotionType;
				spu.promotionPrice = settlementSku.promotionPrice;
			}
			// 设置【满减送】活动
			if (settlementInfo.rewardActivity) {
				spu.rewardActivity = settlementInfo.rewardActivity;
			}
		}
	}
	const getPropertyList = (skus: any): any[] => {
		const result = <any>[]
		for (const sku of skus) {
			if (!sku.properties) {
				continue
			}
			for (const property of sku.properties) {
				// ① 先处理属性
				let resultProperty = <any>result.find((item: any) => item.id === property.propertyId)
				if (!resultProperty) {
					resultProperty = {
						id: property.propertyId,
						name: property.propertyName,
						values: []
					}
					result.push(resultProperty)
				}
				// ② 再处理属性值
				const resultValue = resultProperty.values.find((item: any) => item.id === property.valueId)
				if (!resultValue) {
					resultProperty.values.push({
						id: property.valueId,
						name: property.valueName
					})
				}
			}
		}
		return result
	}
	/**
 * 将分转成元
 *
 * @param price 分，例如说 100 分
 * @returns {string} 元，例如说 1.00 元
 */

	const fen2yuan = (price: number) => {
		return (price / 100.0).toFixed(2);
	}

	// 错误图片展示
	const errorImgFn = (data: any, type: any) => {
		data[type] = '';
	}

	return {
		baseTagStyle: baseTagStyle,
		goodsPrice: goodsPrice,
		priceType: priceType,
		error: errorImgFn,
		fen2yuan: fen2yuan,
		getPropertyList: getPropertyList
	}
}
